require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIHeroChipFilter = {};
HelperFunc.SetUIEnv(UIHeroChipFilter)
local this = UIHeroChipFilter;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBgClose = nil;
this.btnConfirm = nil;
this.btnReset = nil;
this.btnSelect = nil;
this.objSign = nil;
---@type GList
this.listItemGrid = nil;

this.funcConfirm = nil;
this.tableIgnoreHeroChip = nil;
this.isRemainRankChip = nil;

this.heroConfig = nil;
this.heroList = {}

function UIHeroChipFilter.Init(func,filter)--todo 应该小写 暂时先大写
    if heroConfig == nil then
        heroConfig = G.dataTable["t_hero"];
    end
    funcConfirm = func;

    if filter~=nil and filter.list~=nil then
        tableIgnoreHeroChip = filter.list;
    else
        tableIgnoreHeroChip  ={};
    end

    if filter~=nil and filter.isRemainRankChip~=nil then
        isRemainRankChip = filter.isRemainRankChip;
    else
        isRemainRankChip = false;
    end

    local name,package = getUIName(PanelNames.UIHeroChipFilter);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end
---@param obj GComponent
function UIHeroChipFilter.onCreate(obj)
    --gameObject = obj;
    --transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');
    
    if not hasInit then
        hasInit = true;
        btnBgClose = obj:GetChild("BgCloseBtn")
        btnConfirm = obj:GetChild("ConfirmBtn")
        btnReset = obj:GetChild("ResetBtn")
        btnSelect = obj:GetChild("SelectBtn")
        --objSign = obj:GetChild("SelectBtn/Sign")
        listItemGrid = obj:GetChild("itemGrid")
        listItemGrid.itemRenderer = this.setData
        this.addEvent();
        this.poolMgr = logicMgr.PoolManager.New(HeroIcon,true)
    end
    this.refresh();
end

function UIHeroChipFilter.addEvent()
    btnBgClose.onClick:Add(this.onClickClose)
    btnConfirm.onClick:Add(this.onClickConfirm)
    btnReset.onClick:Add(this.onClickReset)
    btnSelect.onClick:Add(this.onClickSelect)

    --[[LuaBehaviour:AddClick(btnBgClose.gameObject, this.onClickClose);
    LuaBehaviour:AddClick(btnConfirm.gameObject, this.onClickConfirm);
    LuaBehaviour:AddClick(btnReset.gameObject, this.onClickReset);
    LuaBehaviour:AddClick(btnSelect.gameObject, this.onClickSelect);]]

end

function UIHeroChipFilter.removeEvent()
    btnBgClose.onClick:Clear()
    btnConfirm.onClick:Clear()
    btnReset.onClick:Clear()
    btnSelect.onClick:Clear()

    --[[LuaBehaviour:RemoveClick(btnBgClose.gameObject);
    LuaBehaviour:RemoveClick(btnConfirm.gameObject);
    LuaBehaviour:RemoveClick(btnReset.gameObject);
    LuaBehaviour:RemoveClick(btnSelect.gameObject);]]

end

function UIHeroChipFilter.LocalListenEvent()

end

function UIHeroChipFilter.LocalCloseEvent()

end

function UIHeroChipFilter.OnDestroy()
    this.removeEvent();
end

function UIHeroChipFilter.OnEnable()
    this.LocalListenEvent();
end

function UIHeroChipFilter.OnDisable()
    this.LocalCloseEvent();
end



function UIHeroChipFilter.refresh()
    --刷新函数
    table.clear(heroList)
    for key, value in pairs(heroConfig) do
        if value.f_HeroPiece~=nil and value.f_HeroPiece>0 then
            local data ={
                star = logicMgr.HeroManager.starToShow(value.f_HeroInitialStar),
                icon = value.f_HeroIcon,
                chip = value.f_HeroPiece,
                quality = logicMgr.HeroManager.starToQuality(value.f_HeroInitialStar),
                state = EnumConst.HeroHeadStateEnum.NONE
            };
            table.insert(heroList,data);
        end
    end
    table.sort(heroList,function (a,b)
        if a.star~= b.star then
            return a.star> b.star;
        end
        return a.chip>b.chip;
    end)
    --listItemGrid:setDataByJsonArray(getJsonStr(heroList));
    listItemGrid.numItems = #heroList
    --objSign:SetActive(isRemainRankChip);
end

function UIHeroChipFilter.setData(index,itemObj)
    index = index + 1

    local itemData = heroList[index]
    ---@type HeroIcon
    local item = this.poolMgr:GetBindScript(itemObj)
    item:Refresh(itemData,this.ClickItem)
    item:SetSelect(table.contains(tableIgnoreHeroChip,itemData.chip))
end
---@param item HeroIcon
function UIHeroChipFilter.ClickItem(item)
    item:SetSelect(not item.isSelect)
    if item.isSelect then
        table.insert(tableIgnoreHeroChip,item.data.chip);
    else
        for i = 1, #tableIgnoreHeroChip do
            if tableIgnoreHeroChip[i] == item.data.chip then
                table.remove(tableIgnoreHeroChip,i);
                break;
            end
        end
    end
end
function UIHeroChipFilter.onClickClose()
    closeUI(PanelNames.UIHeroChipFilter);
end


function UIHeroChipFilter.onClickConfirm()
    local data ={
        list = tableIgnoreHeroChip;
        isRemainRankChip = isRemainRankChip;
    }
    funcConfirm(data);
    closeUI(PanelNames.UIHeroChipFilter);
end


function UIHeroChipFilter.onClickReset()
    tableIgnoreHeroChip = {};
    this.refresh();
end


function UIHeroChipFilter.onClickSelect()
    isRemainRankChip = btnSelect.selected;
    --objSign:SetActive(isRemainRankChip);
end



return UIHeroChipFilter;
